package com.newsnetwork.user.controller;

import com.newsnetwork.api.BaseController;
import com.newsnetwork.api.controller.admin.AppUserMngControllerApi;
import com.newsnetwork.enums.UserStatus;
import com.newsnetwork.grace.result.CommonJSONResult;
import com.newsnetwork.grace.result.ResponseStatusEnum;
import com.newsnetwork.user.service.AppUserMngService;
import com.newsnetwork.user.service.UserService;
import com.newsnetwork.utils.PagedGridResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

/**
 * @author: HuangDekai
 * @date: 2022/11/2 22:10
 * @version: 1.0
 * @since: jdk8
 */

@RestController
public class AppUserMngController extends BaseController implements AppUserMngControllerApi {
    private static final Logger log = LoggerFactory.getLogger(AppUserMngController.class);

    @Autowired
    private AppUserMngService appUserMngService;

    @Autowired
    private UserService userService;

    @Override
    public CommonJSONResult queryAll(String nickname,
                                     Integer status,
                                     Date startDate,
                                     Date endDate,
                                     Integer page,
                                     Integer pageSize) {
        if (page == null) {
            page = COMMON_START_PAGE;
        }
        if (pageSize == null) {
            pageSize = COMMON_PAGE_SIZE;
        }
        PagedGridResult result = appUserMngService.queryAllUserList(nickname, status, startDate, endDate, page, pageSize);

        return CommonJSONResult.ok(result);
    }

    @Override
    public CommonJSONResult userDetail(String userId) {
        return CommonJSONResult.ok(userService.getUser(userId));
    }

    @Override
    public CommonJSONResult freezeUserOrNot(String userId, Integer doStatus) {
        if (!UserStatus.isUserStatusValid(doStatus)) {
            return CommonJSONResult.errorCustom(ResponseStatusEnum.USER_STATUS_ERROR);
        }
        appUserMngService.freezeUserOrNot(userId, doStatus);

        // 刷新用户状态
        // 删除用户会话, 保证用户需要重新登录来刷新其状态
        redisOperator.del(REDIS_USER_INFO + ":" + userId);

        return CommonJSONResult.ok();
    }
}
